<?php

namespace App\Library;

use Illuminate\Support\Facades\Log;
use SeasLog;

class MySeasLog
{
    /**
     * alert
     * @param array $arr
     * @return array
     */
    public static function alert(array $arr): array
    {
        return self::writer($arr, __FUNCTION__);
    }

    /**
     * critical
     * @param array $arr
     * @return array
     */
    public static function critical(array $arr): array
    {
        return self::writer($arr, __FUNCTION__);
    }

    /**
     * emergency
     * @param array $arr
     * @return array
     */
    public static function emergency(array $arr): array
    {
        return self::writer($arr, __FUNCTION__);
    }

    /**
     * debug
     * @param array $arr
     * @return array
     */
    public static function debug(array $arr): array
    {
        return self::writer($arr, __FUNCTION__);
    }

    /**
     * error
     * @param array $arr
     * @return array
     */
    public static function error(array $arr): array
    {
        return self::writer($arr, __FUNCTION__);
    }

    /**
     * info
     * @param array $arr
     * @return array
     */
    public static function info(array $arr): array
    {
        return self::writer($arr, __FUNCTION__);
    }

    /**
     * log
     * @param array $arr
     * @return array
     */
    public static function log(array $arr): array
    {
        return self::writer($arr, __FUNCTION__);
    }

    /**
     * notice
     * @param array $arr
     * @return array
     */
    public static function notice(array $arr): array
    {
        return self::writer($arr, __FUNCTION__);
    }

    /**
     * warning
     * @param array $arr
     * @return array
     */
    public static function warning(array $arr): array
    {
        return self::writer($arr, __FUNCTION__);
    }

    /**
     * writer
     * @param array $arr
     * @param string|null $category
     * @return array
     */
    private static function writer(array $arr, string|null $category = null): array
    {
        $arr = array_merge($arr, [
            'app_name' => config('app.name'),
            'referer' => request()->header('referer'),
            'version' => request()->header('version'),
            'platform' => request()->header('platform'),
            'dateTime' => now()->format('Y-m-d H:i:s.u'),
        ]);
        $category = is_null($category) ? 'info' : strtolower($category);
        if (class_exists(SeasLog::class)) SeasLog::$category(json_encode($arr, JSON_UNESCAPED_UNICODE));
        else Log::$category($category, $arr);
        return $arr;
    }
}
